import { create } from 'zustand'
import { getBanners, getHotRecommend } from '../service/recommend'
import type { bannersResponseData, hotRecommendResponseData } from '../service/recommend'

export interface banner {
    adDispatchJson: null
    adLocation: null
    adSource: null
    adid: null
    bannerBizType: string
    encodeId: string
    event: null
    exclusive: boolean
    extMonitor: null
    extMonitorInfo: null
    imageUrl: string
    monitorBlackList: null
    monitorClick: null
    monitorClickList: null
    monitorImpress: null
    monitorImpressList: null
    monitorType: null
    program: null
    scm: string
    song: null
    targetId: number
    targetType: number
    titleColor: string
    typeTitle: string
    url: string
    video: null
}

export interface hotData {
    id: number
    type: number
    name: string
    copywriter: string
    picUrl: string
    canDislike: boolean
    trackNumberUpdateTime: number
    playCount: number
    trackCount: number
    highQuality: boolean
    alg: string
}

interface RecommendState {
    banners: [] | banner[]
    bannersBackground: [] | string[]
    hotData: [] | hotData[]
    getBannerData: () => Promise<void>
    getHotRecommendData: () => Promise<void>
}

const createRecommendStore = create<RecommendState>((set) => {
    return {
        banners: [],
        hotData: [],
        bannersBackground: [],

        getBannerData: async () => {
            const bannersData = (await getBanners()) as bannersResponseData
            const ban_background: string[] = []
            set({ banners: bannersData.banners }) // 直接设置状态
            bannersData.banners.forEach((item: banner) => {
                ban_background.push(item.imageUrl + '?imageView&blur=40x20')
            })
            set({ bannersBackground: ban_background })
        },

        getHotRecommendData: async () => {
            const hotRecommendData = (await getHotRecommend()) as hotRecommendResponseData
            set({ hotData: hotRecommendData.result }) // 直接设置状态
        }
    }
})

export default createRecommendStore
